import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pp_uikit/pp_uikit_constants.dart';
import 'package:pp_uikit/theme/pp_default_theme.dart';

class PPImageSize {
  final int width;
  final int height;
  double? imageWidth;
  double? imageHeight;

  PPImageSize(this.width, this.height, {this.imageWidth, this.imageHeight});

  static PPImageSize small() {
    return PPImageSize(480, 640);
  }

  static PPImageSize middle() {
    return PPImageSize(768, 1024);
  }

  static PPImageSize big() {
    return PPImageSize(1440, 1920);
  }
}

enum RadiusType {
  all,
  top,
  topLeft,
  topRight,
  bottom,
  bottomLeft,
  bottomRight,
}

class PPNetworkImageConfig {
  static final PPNetworkImageConfig _instance = PPNetworkImageConfig._();

  var pixelRatio = ScreenUtil().pixelRatio ?? 3.0;

  PPNetworkImageConfig._();

  factory PPNetworkImageConfig() {
    return _instance;
  }
}

// ignore: must_be_immutable
class PPNetworkImage extends StatelessWidget {
  // 图片地址
  final String? imageUrl;
  // 圆角
  final double radius;
  final RadiusType radiusType;
  // 请求的图片大小
  PPImageSize? imageSize;
  final double? width;
  final double? height;
  final EdgeInsets margin;
  final bool showPlaceholder;
  final Widget? customPlaceholder;

  /// 边框
  final Border? border;

  PPNetworkImage({
    Key? key,
    required this.imageUrl,
    this.imageSize,
    this.radius = 0.0,
    this.radiusType = RadiusType.all,
    this.width,
    this.height,
    this.showPlaceholder = true,
    this.customPlaceholder,
    this.margin = EdgeInsets.zero,
    this.border,
  }) : super(key: key) {
    imageSize ??= PPImageSize.middle();
    if (width != null) {
      imageSize!.imageWidth = width!;
    }
    if (height != null) {
      imageSize!.imageHeight = height!;
    }
  }

  @override
  Widget build(BuildContext context) {
    if (margin != EdgeInsets.zero) {
      return Container(
        margin: margin,
        height: height,
        width: width,
        child: _buildNetworkImage(),
      );
    }
    return _buildNetworkImage();
  }

  CachedNetworkImage _buildNetworkImage() {
    return CachedNetworkImage(
      imageUrl: _finalUrl(),
      width: width,
      height: height,
      imageBuilder: (context, imageProvider) => Container(
        decoration: BoxDecoration(
          border: border,
          borderRadius: _borderRadius(),
          image: DecorationImage(
            image: imageProvider,
            fit: BoxFit.cover,
          ),
        ),
      ),
      placeholder: showPlaceholder
          ? (context, url) {
              if (customPlaceholder != null) {
                return customPlaceholder!;
              }
              return Image.asset(
                PPDefaultTheme.instance.imagePlaceholder ??
                    'images/icon_empty_loading.png',
                package: PPDefaultTheme.instance.imagePlaceholder == null
                    ? PPUIKitConstant.packageName
                    : null,
                cacheWidth: imageSize!.imageWidth == null
                    ? null
                    : (imageSize!.imageWidth! *
                            PPNetworkImageConfig().pixelRatio)
                        .toInt(),
                cacheHeight: imageSize!.imageWidth == null
                    ? null
                    : (imageSize!.imageWidth! *
                            PPNetworkImageConfig().pixelRatio)
                        .toInt(),
              );
            }
          : null,
      errorWidget: (context, url, error) => Image.asset(
        'images/image_failed.png',
        package: PPUIKitConstant.packageName,
        cacheWidth: imageSize!.imageWidth == null
            ? null
            : (imageSize!.imageWidth! * PPNetworkImageConfig().pixelRatio)
                .toInt(),
        cacheHeight: imageSize!.imageWidth == null
            ? null
            : (imageSize!.imageWidth! * PPNetworkImageConfig().pixelRatio)
                .toInt(),
      ),
    );
  }

  BorderRadius? _borderRadius() {
    var topLeft = Radius.zero;
    var topRight = Radius.zero;
    var bottomLeft = Radius.zero;
    var bottomRight = Radius.zero;
    if (radiusType == RadiusType.all) {
      topLeft = Radius.circular(radius);
      topRight = Radius.circular(radius);
      bottomLeft = Radius.circular(radius);
      bottomRight = Radius.circular(radius);
    } else if (radiusType == RadiusType.top) {
      topLeft = Radius.circular(radius);
      topRight = Radius.circular(radius);
    } else if (radiusType == RadiusType.topLeft) {
      topLeft = Radius.circular(radius);
    } else if (radiusType == RadiusType.topRight) {
      topRight = Radius.circular(radius);
    } else if (radiusType == RadiusType.bottom) {
      bottomLeft = Radius.circular(radius);
      bottomRight = Radius.circular(radius);
    } else if (radiusType == RadiusType.bottomLeft) {
      bottomLeft = Radius.circular(radius);
    } else if (radiusType == RadiusType.bottomRight) {
      bottomRight = Radius.circular(radius);
    }

    return radius > 0
        ? BorderRadius.only(
            topLeft: topLeft,
            topRight: topRight,
            bottomLeft: bottomLeft,
            bottomRight: bottomRight)
        : null;
  }

  String _finalUrl() {
    if (imageUrl == null) {
      return "";
    }
    var width = imageSize!.width;
    var height = imageSize!.height;
    if (imageSize!.imageWidth != null) {
      width =
          (imageSize!.imageWidth! * PPNetworkImageConfig().pixelRatio).toInt();
    }
    if (imageSize!.imageHeight != null) {
      height =
          (imageSize!.imageHeight! * PPNetworkImageConfig().pixelRatio).toInt();
    }
    String resultUrl = imageUrl!.contains("?")
        ? "$imageUrl&x-image-process=image/resize,m_lfit,w_$width,h_$height/quality,Q_90"
        : "$imageUrl?x-image-process=image/resize,m_lfit,w_$width,h_$height/quality,Q_90";
    return resultUrl;
  }
}
